﻿/* *********************************************
 * Crypto libary : MD% module
 * Christiaan Rakowski 20110825
 * This module will compute an MD5 hash of the input string
 * ********************************************/

using System;
using System.Security.Cryptography;
using System.Text;

namespace CryptoCs
{
	namespace Hashing
	{
		class MD5
		{
			public static string GetHash(String input, String salt)
			{
				MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
				if (!String.IsNullOrEmpty(salt))
				{
					input = salt + input + salt;
				}
				byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));				

				StringBuilder sBuilder = new StringBuilder();
				for (int i = 0; i < data.Length; i++)
				{
					sBuilder.Append(data[i].ToString("x2"));
				}
				return sBuilder.ToString();

				//return Encoding.UTF8.GetString(data);
			}
			public static bool VerifyHash(String input, String salt, String hash)
			{
				String hashOfInput = GetHash(input, salt);
				StringComparer comparer = StringComparer.OrdinalIgnoreCase;
				return (0 == comparer.Compare(hashOfInput, hash));
			}
		}
	}
}
